import csv
import json
import time
import random
import requests
from fake_useragent import UserAgent



class CrawlXiecheng():
    def __init__(self):
        # 创建session实例
        self.session = requests.session()
        # 初始化请求
        self.init_request()

    def init_request(self):
        spot_url = "https://you.ctrip.com/sight/hangzhou14/1410601.html"
        self.session.get(url=spot_url)
        self.guid = self.session.cookies['GUID']

        self.post_head = {
            "auth": "",
            "cid": self.guid,
            "ctok": "",
            "cver": "1.0",
            "extension": [{'name': "tecode", 'value': "h5"}],
            "lang": "01",
            "sid": "8888",
            "syscode": "09",
            "xsid": ""
        }

        ua = UserAgent()
        self.headers = {
            'User-Agent': ua.random,
        }

    def generate_params(self):
        params = {
            '_fxpcqlniredt': self.guid,
            'x-traceID': f"{self.guid}-{round(time.time() * 1000)}-{int(1e6 * random.random())}"
        }
        return params

    def crawl_spot_list(self, city_id):
        """
        爬取城市的景点列表
        :return:
        """
        spots_url = f"https://m.ctrip.com/restapi/soa2/18109/json/getAttractionList"
        post_data = {
            "scene": "online",
            "districtId": city_id,
            "index": 1,
            "sortType": 1,
            "count": 10,
            "filter": {"filterItems": []},
            "returnModuleType": "product",
            "head": self.post_head
        }
        response_attractions = self.session.post(url=spots_url, data=json.dumps(post_data),
                                                 params=self.generate_params())
        attractions = response_attractions.json()['attractionList']
        return attractions

    def crawl_spot_comment(self, poi_id,page_index):
        comments_url = f"https://m.ctrip.com/restapi/soa2/13444/json/getCommentCollapseList"
        post_data = {
            "arg": {
                "channelType": 2,
                "collapseType": 0,
                "commentTagId": 0,
                "pageIndex": page_index,
                "pageSize": 10,
                "poiId": poi_id,
                "sourceType": 1,
                "sortType": 3,
                "starType": 0
            },
            "head": self.post_head
        }

        response_comments = self.session.post(url=comments_url, data=json.dumps(post_data),
                                                 params=self.generate_params())
        print(response_comments.json())
        comments = response_comments.json()['result']['items']
        total = response_comments.json()['result']['totalCount']
        return comments,total

    def crawl_xiecheng_page(self):
        return self.session.get("https://you.ctrip.com/").text




